package cn.javaxc.mvcwork.util.common;
import java.io.*;
import java.util.*;

public  class CommonUtil{

  
	public static int parseInt(Object s){
		return parseInt(s,-1);
	}
	
	public static int parseInt(Object s,int _def){
		int i=_def;
		try{
			i=Integer.parseInt(""+s);
		}catch(Exception e){
		}
		return i;
	}
  	public static int parseInt(String s,int idx,int _def){
		if (s!=null){
			String[] values=s.split(",");
			if (values.length>idx){
				return parseInt(values[idx],_def);
			}		
		}
		return _def;
	}
	public static float parseFloat(Object s,float _def){
		float i=_def;
		try{
			i=Float.parseFloat(""+s);
		}catch(Exception e){
		}
		return i;
	}
	public static long parseLong(Object s,long _def){
		long i=_def;
		try{
			i=Long.parseLong(""+s);
		}catch(Exception e){
		}
		return i;
	}
  
	public static double parseDouble(Object s,double _def){
		double i=_def;
		try{
			i=Double.parseDouble(""+s);
		}catch(Exception e){
		}
		return i;
	}
  public static List asList(Object... object_arr){
	return Arrays.asList(object_arr);
  }
  public static String format(String format, Object ... args) {
	return new Formatter().format(format, args).toString();
    }
	public static String join(List list,String str){
		String ret="";
		for (int i=0;i<list.size();i++ ){
			ret+=list.get(i);
			if(i!=list.size()-1)ret+=str;
		}
		return ret;	
	}
	public static String join(Object[] list,String str){
		return join(asList(list),str);	
	}
  public static String toSingleLine(String s) {
		if (s != null) {
			String ret = s.trim();
			ret = ret.replaceAll("\\r\\n", " ");
			ret = ret.replaceAll("\\n\\r", " ");
			ret = ret.replaceAll("\\n", " ");
			ret = ret.replaceAll("\\r", " ");

			return (ret);
		} else {
			return ("");
		}
	}
}
